### v.03 changes:
### introduce menumode and selected variables
### define key events to switch selection if in menumode


###  v.02 changes:
### changes text displayed on screen after a short pause
### creates new main class to initialise the game



import pygame
from pygame.locals import *
import sys
import thread




class BugsMain:
  """ This is the main class which will initialise the Bugs! game"""

  def __init__(self, width = 800, height = 600):  #fixes the size of the game window


 ##initialise pygame
    pygame.init() 

## set screen size 

    self.width = width
    self.height = height

## create the display
    self.screen = pygame.display.set_mode((self.width, self.height))
    pygame.display.set_caption("Bugs!")







  def MainLoop(self):
    ## This is where all the events taking place will run

    ## initiate variables
    menumode = 0 # no menu exists at this stage
    selected = 0 # variable keeping track of which item is selected; 1 = upper menuitem selected, -1 = lower item selected, 0 no mneuitem exists at this stage

    ## create background

    self.background = pygame.Surface(self.screen.get_size()) #ensures background is the same size as the game window
    self.background = self.background.convert()
    self.background.fill((0, 0, 0))

    ## make the background visible and refresh
     
    self.screen.blit(self.background, (0, 0))
    pygame.display.flip()


   ### add opening text and display for 5 seconds

    self.Font = pygame.font.Font(None, 48)   
    self.textOpen = self.Font.render("Splat the bugs and win fabulous prizes!", 1, (255, 10, 10))
    self.textOpenPos = self.textOpen.get_rect()
    self.textOpenPos.centerx = self.background.get_rect().centerx
    self.textOpenPos.centery = self.background.get_rect().centery

    self.screen.blit(self.textOpen, self.textOpenPos)  
    pygame.display.update()


    pygame.time.delay(5000)



    #### erase the opening text from the screen and display menu
    

    self.textErase = pygame.Surface(self.textOpen.get_size())
    self.textErase.fill((0, 0, 0))
    
    self.screen.blit(self.textErase, self.textOpenPos)
    pygame.display.flip() 
 
  
    self.textStart = self.Font.render("START", 1, (166, 166, 166))
    self.textQuit = self.Font.render("QUIT", 1, (166, 166, 166))
    self.textStartSelected = self.Font.render("START", 1, (255, 124, 124))
    self.textQuitSelected = self.Font.render("QUIT", 1, (255, 124, 124))

    self.textStartPos = self.textStart.get_rect()
    self.textStartPos.centerx = self.background.get_rect().centerx
    self.textStartPos.centery = self.background.get_rect().centery - 50


    self.textQuitPos = self.textQuit.get_rect()
    self.textQuitPos.centerx = self.background.get_rect().centerx
    self.textQuitPos.centery = self.background.get_rect().centery + 50

    self.textQErase = pygame.Surface(self.textQuit.get_size())
    self.textQErase.fill((0, 0, 0))

    self.textSErase = pygame.Surface(self.textStart.get_size())
    self.textSErase.fill((0, 0, 0))


    menumode = 1 #menu now selectable
    selected = 1 # 'Start' selected by default

    self.screen.blit(self.textStartSelected, self.textStartPos)
    self.screen.blit(self.textQuit, self.textQuitPos)
    pygame.display.flip()




  ##testing event loop
    while 1:
      for event in pygame.event.get():
        if event.type == QUIT:
          return

        elif event.type == KEYDOWN:
          if event.key == K_ESCAPE:
            return

          elif menumode == 1:
            if event.key == K_UP:
              if selected == -1:
                self.screen.blit(self.textSErase, self.textStartPos)
                self.screen.blit(self.textQErase, self.textQuitPos)
                pygame.display.flip()

                self.screen.blit(self.textStartSelected, self.textStartPos)
                self.screen.blit(self.textQuit, self.textQuitPos)
                pygame.display.flip()
                
                selected = 1


            
            if event.key == K_DOWN:
              if selected == 1:
                self.screen.blit(self.textSErase, self.textStartPos)
                self.screen.blit(self.textQErase, self.textQuitPos)
                pygame.display.flip()

                self.screen.blit(self.textStart, self.textStartPos)
                self.screen.blit(self.textQuitSelected, self.textQuitPos)
                pygame.display.flip()
                
                selected = -1


            if event.key == K_RETURN:
              if selected == -1:
                sys.exit("bye-bye!")


  


if __name__ == '__main__':
  MainWindow = BugsMain(800, 600)
  MainWindow.MainLoop()
  









  
  
  
  
  

